<template>
  <div>
    <el-dialog
      :title="`${!isEdit?'添加':'编辑'}角色`"
      :visible="dialogVisible"
      @close="$emit('watchChild')"
      @open="openPage">
      <el-form :model="roleForm" class="dialog_form">
        <el-form-item label="角色名称:" :label-width="formLabelWidth">
          <el-input v-model="roleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="角色编码:" :label-width="formLabelWidth">
          <el-input v-model="roleForm.code"></el-input>
        </el-form-item>
        <el-form-item label="描述:" :label-width="formLabelWidth">
          <el-input type="textarea" v-model="roleForm.description"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="$emit('watchChild')">取 消</el-button>
        <el-button type="primary" @click="onConfirms">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { finduser, saveOrUpdate, SaveRoles } from '@/services/role'
import Vue from 'vue'
export default Vue.extend({
  name: 'AddRoles',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    isEdit: {
      type: Boolean,
      default: false
    },
    // c_name: {
    //   type: String
    // },
    // c_code: {
    //   type: String
    // },
    // c_description: {
    //   type: String
    // },
    c_id: {
      type: Number
    }
  },
  data () {
    return {
      roleForm: {
        name: '',
        code: '',
        description: ''
      },
      formLabelWidth: '100px'
    }
  },
  methods: {
    async openPage () {
      if (this.isEdit) {
        // this.roleForm.name = this.c_name
        // this.roleForm.code = this.c_code
        // this.roleForm.description = this.c_description;
        // (this.roleForm as SaveRoles).id = this.c_id
        const { data } = await finduser(this.c_id)
        // console.log('编辑', data)
        this.roleForm = data.data
      } else {
        this.roleForm.name = ''
        this.roleForm.code = ''
        this.roleForm.description = ''
      }
    },
    onConfirms () {
      this.$confirm(`此操作将${!this.isEdit ? '添加' : '编辑'}一项角色, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data } = await saveOrUpdate(this.roleForm)
        // console.log('添加分类', data)
        if (data.code === '000000') {
          this.$message.success(`${!this.isEdit ? '添加' : '编辑'}成功`)
          // this.dialogFormVisible = false
          this.$emit('watchChild')
        }
      }).catch(() => {
        this.$message.error('添加失败，请重试')
      })
    }
  }
})
</script>

<style lang="scss" scoped>
.dialog_form{
  padding-right: 5rem;
}

</style>
